<?php

namespace App\Services;

use App\Models\Merchant;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;

class MerchantService
{
    /**
     * Get Merchant List
     * @param int $page
     * @param int $per_page
     * @param int $keyword_search_id
     * @return LengthAwarePaginator
     */
    public static function getList(int $page, int $per_page, int $keyword_search_id): LengthAwarePaginator
    {
        return Merchant::query()
            ->where('keyword_search_id', $keyword_search_id)
            ->paginate($per_page, $columns = ['*'], $pageName = 'page', $page);
    }

    /**
     * Get Merchant By Keyword Search Id
     * @param int $keyword_search_id
     * @return Collection
     */
    public static function getMerchantsByKeywordSearchId(int $keyword_search_id): Collection
    {
        return Merchant::query()->where('keyword_search_id', $keyword_search_id)->get();
    }

    /**
     * Store Merchant
     * @param array $params
     * @return Merchant|Model
     */
    public static function storeMerchant(array $params): Merchant|Model
    {
        return Merchant::query()->create($params);
    }

    /**
     * Get Merchant
     * @param $map
     * @param $title
     * @param $address
     * @param $province
     * @param $city
     * @param $district
     * @return Merchant|null
     */
    public static function getMerchant($map, $title, $address, $province, $city, $district): ?Merchant
    {
        return Merchant::query()
            ->where('map', $map)
            ->where('title', $title)
            ->where('address', $address)
            ->where('province', $province)
            ->where('city', $city)
            ->where('district', $district)
            ->first();
    }

    /**
     * Get random merchant
     * @return Collection
     */
    public static function randomMerchant(): Collection
    {
        return Merchant::query()->inRandomOrder()->limit(3)->get();
    }
}
